package controller

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"mygo/app/models"
	"mygo/app/services"
)

type wxCtl struct {
	db *gorm.DB
}

var Wx = wxCtl{}

func (ctl *wxCtl) GetForeDetailByCode(c *gin.Context) {
	ctl.db = models.Db
	code := c.Query("code")
	foreList := services.GetForeListByCode(code, ctl.db)
	var foreDetail models.ForeDetailJson
	//获取根据time排序的最新的一条数据
	if ctl.db.Where("foreListId = ? AND state = ?", foreList.Id, "0").Order("time desc").First(&foreDetail).RowsAffected == 0 {
		c.JSON(200, gin.H{
			"code":    0,
			"message": "Fail",
			"data":    nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code":    200,
		"message": "Success",
		"data":    foreDetail,
	})
}

// getsignature
func (ctl *wxCtl) GetSignature(c *gin.Context) {
	signature := c.Query("signature")
	timestamp := c.Query("timestamp")
	nonce := c.Query("nonce")
	echostr := c.Query("echostr")
	if CheckSignature(signature, timestamp, nonce) {
		c.String(200, echostr)
	}
	c.String(200, "")
}

// CheckSignature
func CheckSignature(signature, timestamp, nonce string) bool {
	return true
}
